# -*- coding: utf-8 -*-
# Powered By: ECHO
# @Time : 2020/12/20 12:38
import jsonpath
import requests

from HTTP.comm.str_to_dict import str_to_dict
from common.logger import logger
from common.newExcel import Reader, Writer
#数据来源es
#楼盘id
session = requests.session()
datas = session.post("http://10.101.40.139:9200/hdb-prod-build-v3/_search",json={"query" : { "match" : { "_id" : "1695693385039705989" }}})
datas = datas.text
actual_vaules = datas
vaules = str_to_dict(actual_vaules)
logger.info(vaules)

reader = Reader()
reader.open_excel("./cases/es_test_case.xlsx")
writer = Writer()
writer.copy_open("./cases/es_test_case.xlsx","./cases/es_test_case_res.xlsx")

#设置读写excel表格的页签 这个步骤是必须的
reader.set_sheet(reader.get_sheets()[0])
writer.set_sheet(writer.get_sheets()[0])
lines = reader.readline()
logger.info(lines)
for i in range(1,reader.rows):
    line = lines[i]
    #如果第二列是空值等特殊字符 就不是用例 跳过
    if (line[1] == "") or (line[1] == "/") or (line[1] == None):
        pass
    else:
        key = line[0]
        exp_vaule = line[1]
        actual_vaule = str(jsonpath.jsonpath(vaules,"$..%s"%(key))[0])
        logger.info(actual_vaule)
        writer.write(i,2,actual_vaule)
        #assert
        if exp_vaule == actual_vaule:
            logger.info("PASS")
            writer.write(i,3,"PASS",3)
        else:
            logger.info("FAIL")
            writer.write(i,3,"FAIL",2)

writer.save_close()
